package com.ququ.syb.balance.service;

import com.ququ.syb.balance.dao.AccountChannelBalanceDao;
import com.ququ.syb.balance.redis.RedisDao;
import com.ququ.syb.common.constant.RedisKeyConstant;
import com.ququ.syb.common.pojo.AccountChannelBalance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@EnableTransactionManagement
public class AccountChannelbalanceService{

    private Logger logger = LoggerFactory.getLogger(AccountChannelbalanceService.class);

    @Autowired
    private AccountChannelBalanceDao accountChannelBalanceDao;
    @Autowired
    protected AccountChannelBalanceIdSequenceService accountChannelBalanceIdSequenceService;



    @Transactional
    @Cacheable(value = "cache", key = "'balance:'+#accountid+':'+#channelid", unless = "#result == null")
    public AccountChannelBalance getAccountChannelBalance(String accountid,String channelid){
        AccountChannelBalance accountChannelBalance = accountChannelBalanceDao.getAccountChannelBalance(accountid, channelid);
        if (accountChannelBalance == null){
            accountChannelBalance = new AccountChannelBalance();
            accountChannelBalance.setId(accountChannelBalanceIdSequenceService.getNextId());
            accountChannelBalance.setAccountid(accountid);
            accountChannelBalance.setChannelid(channelid);
            accountChannelBalance.setBalance(0L);
            accountChannelBalance.setLastmodifytime(new Date());
            accountChannelBalanceDao.save(accountChannelBalance);
        }
        return accountChannelBalance;
    }

    @Transactional
    @CachePut(value = "cache", key = "'balance:'+#accountid+':'+#channelid")
    @CacheEvict(value = "cache",key = "'balance:account:'+#accountid")
    public AccountChannelBalance addBalance(String accountid,String channelid,Long amount){
        AccountChannelBalance accountChannelBalance = accountChannelBalanceDao.getAccountChannelBalance(accountid, channelid);
        if (accountChannelBalance == null){
            accountChannelBalance = new AccountChannelBalance();
            accountChannelBalance.setId(accountChannelBalanceIdSequenceService.getNextId());
            accountChannelBalance.setAccountid(accountid);
            accountChannelBalance.setChannelid(channelid);
            accountChannelBalance.setBalance(amount);
            accountChannelBalance.setLastmodifytime(new Date());
            accountChannelBalanceDao.save(accountChannelBalance);
        }else
        accountChannelBalanceDao.addBalance(accountid,channelid,amount);
        return accountChannelBalanceDao.getAccountChannelBalance(accountid, channelid);
    }

    @Cacheable(value = "cache",key = "'balance:account:'+#accountid", unless = "#result == null")
    public List<AccountChannelBalance> listAccountChannelBalance(String accountid){
        return accountChannelBalanceDao.getAccountChannelBalances(accountid);
    }

}
